<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="lib/jquery/jquery-3.3.1.min.js"></script>


    <link rel="stylesheet" href="js/ol.css">
    <script src="js/ol-debug.js"></script>
    <link rel="stylesheet" href="css/xcy.css">

</head>
<body>
<div id="map" style="width: 100%">
    <div id="popup" class="ol-popup">
        <a href="#" id="popup-closer" class="ol-popup-closer"></a>
        <div id="popup-title" class="popup-title"></div>
        <div id="popup-content" class="popup-content"></div>
    </div>
</div>
<script>
    var format = 'image/png';
    //view
    var view=new ol.View({
        // 设置成都为地图中心
        center: ol.proj.transform([110,39],"EPSG:4326","EPSG:3857"),
        zoom: 4
    });
    ////vector
    var vectorSource = new ol.source.Vector({
        features:[]
    });

    var vectorLayer = new ol.layer.Vector({
        source: vectorSource,
        style: styleFunction
    });
    function  styleFunction(feature) {

        return  new ol.style.Style({
            image: new ol.style.Circle({
                radius: 5,
                fill: new ol.style.Fill({
                    color: "red",
                    opacity: 0.5
                })
            })
        });
    }

    // map
    var map = new ol.Map({
        layers: [
            getTdtLayer("vec_w"),
            getTdtLayer("cva_w")
        ],
        view:view,
        target: 'map'
    });
    map.addLayer(vectorLayer);
    ////overlay
    var container = $("#popup");
    var content = $("#popup-content");
    var title =$("#popup-title");
    var closer =$("#popup-closer");
    closer.on("click",function () {
        container.hide();
        closer.blur();
        return false;
    });
    var overlay = new ol.Overlay({
        element: container[0],
        autoPan:true,
        autoPanMargin:100,
        positioning:'bottom-center'
    });
    map.addOverlay(overlay);

    map.on('click', function(evt) {
        var coordinate = evt.coordinate;
        var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(coordinate, 'EPSG:3857', 'EPSG:4326'));
        var info='<p>You clicked here:</p><code>' + hdms + '</code>';
        showinfo(coordinate,info);
        //
        var feature=new ol.Feature({
            geometry:new ol.geom.Point(coordinate)
        });
        vectorSource.clear();
        vectorSource.addFeature(feature)

    });
    function showinfo(coordinate,info) {
        content.html(info) ;
        container.show();
        title.html("提示信息");
        title.show();
        overlay.setPosition(coordinate);
    }

    function getTdtLayer(lyr) {
        var urls=[];
        for(var i=0;i<8;i++)
        {
            urls.push( "http://t"+i+".tianditu.com/DataServer?T=" + lyr + "&X={x}&Y={y}&L={z}")
        }
        var layer = new ol.layer.Tile({
            source: new ol.source.XYZ({
                urls:urls
            })
        });
        return layer;
    }
</script>
</body>
</html>